#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
#include <string.h>
//从栈底到栈顶遍历，打印输出
static void stackDump (lua_State *L) 
{
    int i;
    int top = lua_gettop(L);
    for (i = 1; i <= top; i++) 
    { /* repeat for each level */
        int t = lua_type(L, i);
        switch (t) 
        {
            case LUA_TSTRING: /* strings */
                printf("%02d %-10s: `%s'\r\n",i,lua_typename(L,t), lua_tostring(L, i));
                break;
            case LUA_TBOOLEAN: /* booleans */
                printf(lua_toboolean(L, i) ? "%02d %-10s: true\r\n" : "%02d %-10s: false\r\n",i,lua_typename(L,t) );
                break;
            case LUA_TNUMBER: /* numbers */
                printf("%02d %-10s: %g\r\n",i,lua_typename(L,t), lua_tonumber(L, i));
                break;
            case LUA_TNIL:
                printf("%02d %-10s: nil\r\n",i,lua_typename(L,t));
                break;
            default: /* other values */
                printf("%02d other type values: %-10s\r\n",i, lua_typename(L, t));
                break;
        }
    }
    printf("\n"); /* end the listing */
}
int main( void )
{
    lua_State *L  = luaL_newstate();
    if(!L)
        printf("luaL_newstate fail \n");
    printf("luaL_newstate ok \n");
    // luaopen_base(L);    /* opens the basic library */
    // luaopen_table(L);   /* opens the table library */
    // luaopen_io(L);      /*opens the I/O library */
    // luaopen_string(L);  /* opens the string lib. */
    // luaopen_math(L);    /* opens the math lib. */
    // lua_pushboolean(L, 1); 
    // lua_pushnumber(L, 10);
    // lua_pushnil(L); 
    // lua_pushstring(L, "hello");
    // //入栈操作  
    // lua_pushstring(L, "I am so cool~");   
    // lua_pushnumber(L,20);  
    // printf("state top=%d\n",lua_gettop(L) );
    // //取值操作  
    // if(lua_isstring(L,1) )
    // {  //判断是否可以转为string   
    //     printf("%s\n",lua_tostring(L,1) );
    // }  
    // if( lua_isnumber(L,2))
    // {  
    //     printf( "number=%f\n",lua_tonumber(L,2) );
    // }
    // printf( "LUA State date ......\n");
    // stackDump(L);
    // lua_settop(L, 9);
    stackDump(L); 
    printf("state top=%d\n",lua_gettop(L) );
    if(luaL_loadfile(L, "./io.lua"))  
    {  
        perror("loadfile failed");  
        return -1;  
    }
    printf("state top=%d\n",lua_gettop(L) );
    stackDump(L); 
     lua_pcall(L, 0, 0, 0);
    //关闭state  
    lua_close(L);
}